﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
namespace John
{
    public partial class viste_productadd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检查
            //HttpCookie cookie = Request.Cookies["jisuanji110"];
            var session = Session["name"];
            if (session == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }

            if (!IsPostBack)
            {
                getCategory();//绑定类别下拉列表           
            }
        }
        private void getCategory()//绑定类别下拉列表
        {
            DBConn myDB = new DBConn();
            string sql = "select * from Category";
            ddlCategory.DataSource = myDB.getDataReader(sql);
            ddlCategory.DataTextField = "CName";
            ddlCategory.DataValueField = "CID";
            ddlCategory.DataBind();
            myDB.Close();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strName = txtName.Text.Trim();
            string strCID = ddlCategory.SelectedValue;           
            string strNPrice = txtNPrice.Text.Trim();
            string strBewrite = txtBewrite.Text.Trim();
            if (strName == String.Empty ||  strCID == String.Empty|| strNPrice == String.Empty)
            {
                Response.Write("<script>");
                Response.Write("alert('必选项不能为空!!!');");
                Response.Write("</script>");
                return;
            }
            else if (strName.Length > 35)
            {
                Response.Write("<script>");
                Response.Write("alert('输入产品名称太长了!!!');");
                Response.Write("</script>");
                return;
            }
          
            try
            {
                double.Parse(strNPrice);
            }
            catch
            {
                Response.Write("<script>");
                Response.Write("alert('请检查 单价 的格式!!!');");
                Response.Write("</script>");
                return;
            }

            if (uploadFile.PostedFile.FileName.Trim() != String.Empty && (Path.GetExtension(uploadFile.PostedFile.FileName) != ".gif" && Path.GetExtension(uploadFile.PostedFile.FileName) != ".jpg"))
            {
                Response.Write("<Script>alert('上传的图片格式必须为.gif或.jpg!!')</Script>");
                return;
            }
            try
            {
                if (uploadFile.PostedFile.FileName.Trim() != String.Empty)
                {
                    string nam = uploadFile.PostedFile.FileName;
                    //取得文件名(抱括路径)里最后一个"."的索引
                    int i = nam.LastIndexOf(".");
                    //取得文件扩展名
                    string newext = nam.Substring(i);
                    //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
                    DateTime now = DateTime.Now;
                    string newname = now.DayOfYear.ToString() + uploadFile.PostedFile.ContentLength.ToString();
                    uploadFile.PostedFile.SaveAs(Server.MapPath(".\\UPloadImg\\" + newname + newext));
                    string imgname = newname + newext;
                    string sql;
                    strName = CleanString.htmlInputText(strName);
                    DBConn mydb = new DBConn();
                    sql = "insert into Products(PName,CID,PNPrice,PBewrite,uname,pggxh,PStock,PPicture) values('" +
                            strName + "'," + strCID + "," + strNPrice + ",'" + strBewrite + "','" + Request.Cookies["jisuanji110"].Value + "','" + txtFPrice.Text.Trim() + "','" + TextBox2.Text.Trim() + "','" + imgname + "')";
                    mydb.Insert(sql);
                    mydb.Close();
                    Response.Redirect("productalter.aspx");
                }
                else
                {
                    string sql;
                    strName = CleanString.htmlInputText(strName);
                    DBConn mydb = new DBConn();
                    sql = "insert into Products(PName,CID,PNPrice,PBewrite,uname,pggxh,PStock,PPicture) values('" +
                            strName + "'," + strCID + "," + strNPrice + ",'" + strBewrite + "','" + Request.Cookies["jisuanji110"].Value + "','" + txtFPrice.Text.Trim() + "','" + TextBox2.Text.Trim() + "','showimg.gif')";
                    mydb.Insert(sql);
                    mydb.Close();
                    Response.Redirect("productalter.aspx");
                }
            }
            catch
            {
                //Response.Redirect("Error.aspx");
 
            }
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {

        }
    }
}
